#! /usr/bin/env python
#
# Arcade Tonk Tanks 0.0.7 map Editor
# created on 2009-11-14 by Koen Lefever
# WORK IN PROGRESS - THIS EDITOR IS NOT USABLE YET
#####################################################################

import math
import os.path
import pygame
from pygame.locals import *
from Source import GameData         # contains all global variables
from Source import Graphics         # auxiliary graphics functions
from Source import RespawnPoint     # Respawn point class
from Source import Logo             # Arcade Tonk tanks logo class
from Source import StringOption     # String Option class
from Source import NumericalOption  # Numerical Option class

# respawnpoints = [[400,706,90], [740,15,180],[105,285,270],[340,264,0],[14,714,0],[740,713,180]] # Atlantikwall
# respawnpoints = [[5,7,0], [745,717,180], [5,717,0], [745,7,180], [380,10,270], [380,717,90]] # ground zero
respawnpoints = [[400, 692, 90],[30, 33, 0],[395, 32, 270],[25, 392, 90],[459, 376, 0],[724, 709, 180]] # the river

#####################################################################
class BattleGround():
    def __init__(self, name, image):
        name = name
        background = image 
        walls = []
        water = []
        respawn_points = respawnpoints

#####################################################################
class MousePos(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.font = pygame.font.Font("freesansbold.ttf", 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lasttext = -1
        mx, my = pygame.mouse.get_pos()
        self.text = str(mx) + "," + str(my)
        self.update()
        self.rect = self.image.get_rect().move(x, y)


    def update(self):
        mx, my = pygame.mouse.get_pos()
        self.text = str(mx) + "," + str(my)
        if self.text != self.lasttext:
            self.lasttext = self.text
            self.image = self.font.render(self.text, 0, self.color)

#####################################################################

def main(winstyle = 0):
    # Input
    print "---------------------------------------------------------"
    print "---      Arcade Tonk Tanks - Battleground Editor      ---"
    print "---------------------------------------------------------"
    #print "Please enter a new name for your Battlegroud."
    #battleground_name = raw_input('Battleground name: ')
    battleground_name = 'test'
    #print "Please choose a background image."
    #background_filename = raw_input('Background image filename: ')
    # background_filename = 'Atlantikwall.png'
    # background_filename = 'Arcade_tanks_background.jpg'
    background_filename = 'River.png'
    
    # Initialize pygame
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode(GameData.screenrect.size)
    RespawnPoint.images = Graphics.load_images('respawn_point_0.bmp','respawn_point_1.bmp',
                                               'respawn_point_2.bmp','respawn_point_3.bmp',
                                               'respawn_point_4.bmp','respawn_point_5.bmp')
    Logo.images = Graphics.load_images('Editor_0.bmp','Editor_1.bmp','Editor_2.bmp')

    #decorate the game window
    #icon = pygame.transform.scale(Tank.images[6], (32, 32))
    #pygame.display.set_icon(icon)
    pygame.display.set_caption('Arcade Tonk Tanks battleground editor')
    pygame.mouse.set_visible(1)

    #create the background: tile the bgd image & draw the game maze
    bgdtile = Graphics.load_background(background_filename, None)
    background = pygame.Surface(GameData.screenrect.size)
    battleground = BattleGround(battleground_name, background_filename)
    for x in range(0, GameData.screenrect.width, bgdtile.get_width()):
        background.blit(bgdtile, (x, 0))
    screen.blit(background, (0,0))
    pygame.display.flip()
    
    # Initialize Game Groups
    respawn_points = pygame.sprite.Group()
    logos = pygame.sprite.Group()
    mousepos = pygame.sprite.Group()
    all = pygame.sprite.RenderUpdates()
    
    #assign default groups to each sprite class
    RespawnPoint.containers = respawn_points, all
    Logo.containers = logos, all
    NumericalOption.containers = all
    StringOption.containers = all
    MousePos.containers = mousepos, all

    # Create the respawn points
    for respawn_point in respawnpoints:
        RespawnPoint(respawn_point[0],respawn_point[1],respawn_point[2])
    Logo()
    MousePos(850,130)
    step = 0

    #####################################################################
    # main loop
    while True:
        all.clear(screen, background)
        

        waiting = True

        drawstate = 'idle'
        while waiting:
            #get input
            for event in pygame.event.get():
                if event.type == QUIT or \
                    (event.type == KEYDOWN and event.key == K_ESCAPE):
                        return
            keystate = pygame.key.get_pressed()
            if keystate[K_RETURN] or keystate[K_KP_ENTER]:
                waiting = False
            if event.type == MOUSEBUTTONDOWN:
                if drawstate == 'idle':
                    if event.button == 1:
                        drawstate = 'drawing'
                        corner1x, corner1y = pygame.mouse.get_pos()
                        
            if event.type == MOUSEBUTTONUP:
                if drawstate == 'drawing':
                    corner2x, corner2y = pygame.mouse.get_pos()
                    if corner1x < corner2x:
                        cornerx = corner1x
                        width = corner2x - corner1x
                    else:
                        cornerx = corner2x
                        width = corner1x - corner2x
                    if corner1y < corner2y:
                        cornery = corner1y
                        height = corner2y - corner1y
                    else:
                        cornery = corner2y
                        height = corner1y - corner2y
                    pygame.draw.rect(background, (33,33,33), Rect(cornerx, cornery, width, height), 5)
                    print 'Rect(', cornerx, ',', cornery, ',', width, ',', height, '),'
                    screen.blit(background, (0,0))
                    pygame.display.flip()
                    drawstate = 'idle'
            step = step + 1
            if step > 4:
                step = 0
                GameData.animstep = GameData.animstep + 1
                if GameData.animstep > 5:
                    GameData.animstep = 0
            # clear/erase the last drawn sprites
            all.clear(screen, background)
            respawn_points.update()
            logos.update()
            mousepos.update()
            #draw the scene
            dirty = all.draw(screen)
            pygame.display.update(dirty)
            #cap the framerate
            clock.tick(40)

        return

#####################################################################
#call the "main" function if running this script
if __name__ == '__main__': main()


    
